package com.changgou.business.controller;

import com.changgou.business.pojo.Time;
import com.changgou.business.service.TimeService;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//tb_seckill_time表
@RestController
@CrossOrigin
@RequestMapping("/time")
public class TimeController {

    @Autowired
    private TimeService timeService;
    @GetMapping
    public Result findAll(){
        try {
            List<Time> times = timeService.findAll();
            return new Result(true,StatusCode.OK,"查询时间段成功",times);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询时间段失败");
        }
    }

    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        timeService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 添加秒杀时间段
     * @param time
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Time time){
        try {
            timeService.add(time);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 时间段的启用、关闭
     * @param time
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Time time){
        try {
            timeService.updateStatus(time);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

}
